<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-3
 * Time: 下午10:17
 */
//数据统计
namespace Admin\Controller;
use Mcit\Page;

class StatisticsController extends BaseController {

    //首页
    public function index(){

        $map['state']       = 1;
        //充电站总数
        $this->countPlant   = M("plant")->where(1)->count();
        //充电桩总数
        $this->countChar    = M("char")->where(1)->count();//电桩总数
        //充电总次数
        $this->countCharge  = M("bill_total")->where($map)->sum("total");
        //预约总次数
        $this->countBespeak = M("bespeak_total")->where($map)->sum("total");
        //账单总数
        $this->countBill    = M("bill_total")->where($map)->sum("amount");
        $this->display();
    }

    //所有省的站点统计
    public function plant(){
        $this->plant_online = M("plant")->count();//全国总计
        $china = D("Region")->where("level=1")->select();
        foreach($china as $k=>$v){
            $map['location'] = array('like','%'.$v['code'].'%');
            $china[$k]['num'] = M("plant")->where($map)->count();
        }
        $this->china= $china;
        //P($china);
        $this->display();
    }
    //所在省下的所有市站点统计
    public function city(){
        $map['location'] = array('like','%'.I("city").'%');
        $this->city = M("plant")->where($map)->count();

        //分页
        $count = M("region")->where("ppcode='".I("city")."' && level=2")->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $china = M("region")->where("ppcode='".I("city")."' && level=2")->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束

        //$china = D("Region")->where("ppcode='".I("city")."'")->select();
        //P($china);
        foreach($china as $k=>$v){
            $map['location'] = array('like','%'.$v['code'].'%');
            $china[$k]['num'] = M("plant")->where($map)->count();
        }
        $this->china= $china;
        $this->display();
    }
    //所在市下的所有区的站点统计
    public function district(){
        $map['location'] = array('like','%'.I("district").'%');
        $this->city = M("plant")->where($map)->count();
        $china = D("Region")->where("pcode='".I("district")."' && level=3")->select();
        $this->china= $china;

        $plant = M("plant")->where($map)->select();
        foreach($plant as $k=>$v){
            //将数字的地址转换成文字
            $region = strsToArray($v['location']);
            $regionOne = D("Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $plant[$k]['location'] = $regionOne['fullname'];
            if(mb_strlen($v['name'],'UTF8') <=17 ){
                $plant[$k]['name_text'] = $v['name'];
            }else{
                $plant[$k]['name_text'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
        }
        $this->plant = $plant;
        if(IS_AJAX){
            $district=$_POST['district'];
            if($district == $_POST['code']-1){
                $mapp['location'] = array('like','%'.$district.'%');
            }else{
                $mapp['location'] = array('like','%'.$_POST['code'].'%');
            }
            $plants = M("plant")->where($mapp)->select();
            /*if(empty($plants)){
                $code = $_POST['code']-1;
                $mapp['location'] = array('like','%'.$code.'%');
                $plants = M("plant")->where($mapp)->select();
            }*/
            foreach($plants as $k=>$v){
                //将数字的地址转换成文字
                $region = strsToArray($v['location']);
                $regionOne = D("Region")->where("code='".$region[2]."'")->field("fullname")->find();
                $plants[$k]['location']  = $regionOne['fullname'];
                if(mb_strlen($v['name'],'UTF8') <=17 ){
                    $plants[$k]['name_text'] = $v['name'];
                }else{
                    $plants[$k]['name_text'] = mb_substr($v['name'],0,17,"utf-8")."···";
                }
                if($v[evc_type] == 1){
                    $plants[$k]['evc_type'] = "直流";
                }else{
                    $plants[$k]['evc_type'] = "交流";
                }
                if($v[booking] == 1){
                    $plants[$k]['booking'] = "支持";
                }else{
                    $plants[$k]['booking'] = "不支持";
                }
                if($v[current_state] == 1){
                    $plants[$k]['current_state'] = '<a class="btn btn-success btn-xs">运营中</a>';
                }elseif($v[current_state] == 2){
                    $plants[$k]['current_state'] = '<a class="btn btn-warning btn-xs changehh">安装中</a>';
                }else{
                    $plants[$k]['current_state'] = '<a class="btn btn-warning btn-xs changehh">规划中</a>';
                }
                if($v[is_active] == 1){

                    $plants[$k]['is_active'] = '<img src="'.__ROOT__.'/Public/img/gou.png" alt="是"/>';
                }else{
                    $plants[$k]['is_active'] = '<img src="'.__ROOT__.'/Public/img/cha.png" alt="否"/>';
                }
            }
            $return = array();
            if(!empty($plants)){
                foreach($plants as $key=>$val){
                    $str = '<tr><td>'.$val[id].'</td><td><span title="'.$val[name].'">'.$val[name_text].'</span></td><td>'.$val[location].'</td><td>'.$val[evc_type].'</td><td>'.$val[booking].'</td><td>'.$val[current_state].'</td><td>'.$val[is_active].'</td><td>'.$val[total_ev].'</td><td>'.$val[total_par].'</td></tr>';
                    $return['html'] .= $str;
                }

            }else{

                $return['html'] = '<td colspan="99" style="text-align:center;" class="danger" id="showORhide"> <strong><i class="fa fa-meh-o"></i>&nbsp;&nbsp;当前条件下没有数据</strong></td>';
            }
            echo json_encode($return);

            exit;
        }
        $this->display();
    }

    //所有省的电桩统计
    public function char_province(){
        $this->charAll = M("char")->count();//全国总计
        $china = D("Region")->where("level=1")->select();
        foreach($china as $k=>$v){
            $map['location'] = array('like','%'.$v['code'].'%');
            $plant           = M("plant")->where($map)->select();
            $str             = '';
            foreach($plant as $s){
                $str    .= $s['id'].",";
                $china[$k]['str']       = strsToArray($str);
                $china[$k]['plant_ids'] = rtrim($str,',');
            }
        }
        foreach($china as $h=>$d){
            $china[$h]['num'] = 0;
            for($i=0;$i<count($d['str']);$i++){
                $china[$h]['num'] += M("char")->where("ev_plant_id='".$d['str'][$i]."'")->count();
            }
        }
        $this->china= $china;
        $this->display();
    }

    //所有城市的电桩统计
    public function char_city(){
        $map['location'] = array('like','%'.I("city").'%');
        $plant = M("plant")->where($map)->select();
        $cityAll = '';
        foreach($plant as $b){

            $cityAll += M("char")->where("ev_plant_id='".$b['id']."'")->count();
        }
        $this->cityAll =$cityAll;

        //分页
        $count = M("region")->where("ppcode='".I("city")."' && level=2")->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $china = M("region")->where("ppcode='".I("city")."' && level=2")->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($china as $k=>$v){
            $map['location'] = array('like','%'.$v['code'].'%');
            $plant           = M("plant")->where($map)->select();
            $str             = '';
            foreach($plant as $s){
                $str    .= $s['id'].",";
                $china[$k]['str']       = strsToArray($str);
                $china[$k]['plant_ids'] = rtrim($str,',');
            }
        }
        foreach($china as $h=>$d){
            $china[$h]['num'] = 0;
            for($i=0;$i<count($d['str']);$i++){
                $china[$h]['num'] += M("char")->where("ev_plant_id='".$d['str'][$i]."'")->count();
            }
        }
        $this->china= $china;
        $this->display();
    }

    //所在城市下所有区下的桩统计
    public function char_area(){

        $map['location'] = array('like','%'.I("district").'%');
        $plant = M("plant")->where($map)->select();
        $area = '';
        $str             = '';
        foreach($plant as $b){
            $area += M("char")->where("ev_plant_id='".$b['id']."'")->count();
            $str    .= $b['id'].",";
        }
        $this->area = $area;
        $china = D("Region")->where("pcode='".I("district")."' && level=3")->select();
        $this->china= $china;

        $condi['ev_plant_id'] = array("IN",rtrim($str,','));
        $char = M("char")->where($condi)->select();
        foreach($char as $key=>$val){
            $plantone = M("plant")->where("id='".$val['ev_plant_id']."'")->find();
            $region = strsToArray($plantone['location']);
            $regionOne = D("Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $char[$key]['location'] = $regionOne['fullname'];
            $char[$key]['name'] = $plantone['name'];
            if(mb_strlen($plantone['name'],'UTF8') <=17 ){
                $char[$key]['name_text'] = $plantone['name'];
            }else{
                $char[$key]['name_text'] = mb_substr($plantone['name'],0,17,"utf-8")."···";
            }
        }
        $this->char = $char;
        if(IS_AJAX){
            $district=$_POST['district'];
            if($district == $_POST['code']-1){
                $mapp['location'] = array('like','%'.$district.'%');
            }else{
                $mapp['location'] = array('like','%'.$_POST['code'].'%');
            }
            $plants = M("plant")->where($mapp)->select();
            $str             = '';
            foreach($plants as $s){
                $str    .= $s['id'].",";
            }
            $condi['ev_plant_id'] = array("IN",rtrim($str,','));
            $char = M("char")->where($condi)->select();
            foreach($char as $key=>$val){
                $plantone = M("plant")->where("id='".$val['ev_plant_id']."'")->find();
                $region = strsToArray($plantone['location']);
                $regionOne = D("Region")->where("code='".$region[2]."'")->field("fullname")->find();
                $char[$key]['location'] = $regionOne['fullname'];
                $char[$key]['name'] = $plantone['name'];
                if(mb_strlen($plantone['name'],'UTF8') <=17 ){
                    $char[$key]['name_text'] = $plantone['name'];
                }else{
                    $char[$key]['name_text'] = mb_substr($plantone['name'],0,17,"utf-8")."···";
                }
                if($val['evc_type'] == 1){
                    $char[$key]['evc_type'] = "直流";
                }elseif($val['evc_type'] == 2){
                    $char[$key]['evc_type'] = "交流";
                }elseif($val['evc_type'] == 3){
                    $char[$key]['evc_type'] = "混合";
                }
                if($val['evc_quantity'] == 1){
                    $char[$key]['evc_quantity'] = "单枪";
                }elseif($val['evc_quantity'] == 2){
                    $char[$key]['evc_quantity'] = "双枪";
                }
            }
            $return = array();
            if(!empty($char)){
                foreach($char as $val){
                    $str = '<tr><td>'.$val[id].'</td><td><span title="'.$val[name].'">'.$val[name_text].'</span></td><td>'.$val[location].'</td><td>'.$val[evc_number].'</td><td>'.$val[key_link].'</td><td>'.$val[evc_type].'</td><td>'.$val[evc_quantity].'</td><td>'.$val[card_wifi].'</td><td>'.$val[sys_creat_time].'</td></tr>';
                    $return['html'] .= $str;
                }
            }else{
                $return['html'] = '<td colspan="99" style="text-align:center;" class="danger" id="showORhide"> <strong style="color: red"><i class="fa fa-meh-o"></i>&nbsp;&nbsp;当前条件下没有数据</strong></td>';
            }
            echo json_encode($return);
            exit;
        }
        $this->display();
    }

    //充电记录列表
    public function charge_list(){

        $start_time = I("start_time");
        if(!empty($_GET['end_time'])){
            $start_time  = date("Y-m-d H:s:i",strtotime($start_time));
            $end_time    = I("end_time");
            $end_time    = date("Y-m-d 23:59:59",strtotime($end_time));
            $map['time'] = array("between",array($start_time,$end_time));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['time'] = array("like","%".$start_time."%");
        }
        if(!empty($_GET['user_card'])){
            $map['user_card'] = I('user_card');
        }
        if(!empty($_GET['key_link'])){
            $map['key_link']  = I('key_link');
        }
        //分页
        $count = D("Charge")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $charge   = D("Charge")->where($map)->order("time desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach ($charge as $k => $v) {
            $char       = M("char")->where("key_link=".$v['key_link'])->find();
            $plant      = M("plant")->where("id=".$char['ev_plant_id'])->find();
            $arr        = strsToArray($plant['location']);
            $region     = M("region")->where("code=".$arr['2'])->find();
            $charge[$k]['region_text']   = $region['fullname'];
            $charge[$k]['plantAddr']     = $plant['name'];
            if(mb_strlen($plant['name'],"utf-8") <= 17){
                $charge[$k]['plantName'] = $plant['name'];
            }else{
                $charge[$k]['plantName'] = mb_substr($plant['name'],0,17,"utf-8")."···";
            }
        }
        $this->charge = $charge;
        $this->display();
    }

    //充电记录详情
    public function charge_detail(){

        $charge = D("Charge")->where("id=".I("id"))->find();
        $char   = M("char")->where("key_link=".$charge['key_link'])->find();
        $plant  = M("plant")->where("id=".$char['ev_plant_id'])->find();
        $charge['evc_number'] = $char['evc_number'];
        $charge['name']       = $plant['name'];
        $charge['address']    = $plant['address'];
        $this->data           = $charge;
        $this->display();
    }

    //充电记录统计
    public function charge_site(){

        $map['state']  = 1;
        $maps['state'] = 1;
        $year          = "currentTime";
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime']  = array("between",array(I("start_time"),I("end_time")));
            $maps['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime']  = array('like',I("start_time").'%');
            $maps['currentTime'] = array('like',I("start_time").'%');
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']   = array("between",array($begin,$end));
            $maps['currentTime']  = array("between",array($begin,$end));
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $maps['currentTime'] = array('like',I("year").'%');
        }
        //区域查询
        if(!empty($_GET['location_f_1'])){

            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
            $map['location']  = array('like','%'.$location.'%');
            $mapp['location'] = array('like','%'.$location.'%');
            //将数字的地址转换成文字
            $region     = strsToArray($location);
            if($region[2] == ''){
                if($region[1] == 110100 || $region[1] == 120100 || $region[1] == 310100 || $region[1] == 500100){
                    $regionOne  = D("Admin/Region")->where("code='".$region[0]."'")->field("fullname")->find();
                }else{
                    $regionOne  = D("Admin/Region")->where("pcode='".$region[1]."'")->field("fullname")->find();
                }
            }else{
                $regionOne  = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            }
            $area = $regionOne['fullname'];
        }
        //站点名称查询
        if(!empty($_GET['name'])){
            $map['site_name'] = array('like','%'.I('name').'%');
            $mapp['name'] = array('like','%'.I('name').'%');
        }
        $title = "数据范围：";
        if(!empty($_GET['location_t_1']) && !empty($_GET['year'])){
            $this->node = $title."按城市+年搜索——".$area."--".$_GET['year'];
        }elseif(!empty($_GET['location_t_1']) && !empty($_GET['month'])){
            $this->node = $title."按城市+月搜索——".$area."--".$_GET['month'];
        }elseif(!empty($_GET['location_t_1']) && !empty($_GET['end_time'])){
            $this->node = $title."按城市+时间段——".$area."--".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['location_t_1']) && !empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."按城市+开始时间——".$area."--".$_GET['start_time'];
        }elseif(!empty($_GET['name'])){
            $this->node = $title."按站点名称——".$_GET['name'];
        }elseif(!empty($_GET['year'])){
            $this->node = $title."按年搜索——".$_GET['year'];
        }elseif(!empty($_GET['month'])){
            $this->node = $title."按月搜索——".$_GET['month'];
        }elseif(!empty($_GET['end_time'])){
            $this->node = $title."时间段——".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."开始时间——".$_GET['start_time'];
        }elseif(!empty($_GET['location_t_1'])){
            $this->node = $title."按城市搜索——".$area;
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bill_total")->field("sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($map)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['electricity'] = $v['electricity'] ? sprintf("%.2f", $v['electricity']) : '0.00';
            $countBill[$k]['carbon']      = sprintf("%.2f", $v['electricity'] * 1.174);
            $countBill[$k]['charge_time'] = $v['charge_time'] ? Sec2Times($v['charge_time']) : '0小时0分0秒';
            $countBill[$k]['amount']      = $v['amount'] ? sprintf("%.2f", $v['amount']) : '0.00';
            $countBill[$k]['service_fee'] = $v['service_fee'] ? sprintf("%.2f", $v['service_fee']) : '0.00';
            $countBill[$k]['total']       = $v['total'] ? $v['total'] : 0;
        }
        $this->countBills = $countBill;//汇总输出

        //分页
        $count = M("plant")->where($mapp)->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $plant = M("plant")->field("id,name,location,address,total_ev")->where($mapp)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($plant as $k=>$v){
            $maps['site_id'] = $v['id'];
            //将数字的地址转换成文字
            $region = strsToArray($v['location']);
            $regionOne = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $plant[$k]['location'] = $regionOne['fullname'];
            if(mb_strlen($v['name'],"utf-8") <= 17){
                $plant[$k]['name_text'] = $v['name'];
            }else{
                $plant[$k]['name_text'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
            $sumdata = M("bill_total")->field("sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($maps)->select();
            foreach($sumdata as  $vs){
                $plant[$k]['electricity'] = $vs['electricity'] ? sprintf("%.2f", $vs['electricity']) : '0.00';
                $plant[$k]['carbon']      = sprintf("%.2f", $vs['electricity'] * 1.174);
                $plant[$k]['charge_time'] = $vs['charge_time'] ? Sec2Times($vs['charge_time']) : '0小时0分0秒';
                $plant[$k]['amount']      = $vs['amount'] ? sprintf("%.2f", $vs['amount']) : '0.00';
                $plant[$k]['service_fee'] = $vs['service_fee'] ? sprintf("%.2f", $vs['service_fee']) : '0.00';
                $plant[$k]['total']       = $vs['total'] ? $vs['total'] : 0;
            }
        }
        $this->china = D("Region")->getChinaArea();//全国省市区
        $this->assign("plant",$plant);//数据输出
        $this->display();
    }

    //站点充电数据详情
    public function site_charge(){

        $id   = I("id");
        $uid  = session('uid');
        $pid  = session('pid');
        $type = session('fac_type');
        $key  = session('fac_key');
        $this->pro = M("plant")->where("id={$id}")->getField("name");
        $map['state']    = 1;
        $map['site_id']  = $id;
        $year            = "currentTime";
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = array('like',I("start_time").'%');
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $year  = "substring_index(currentTime,'-',2)";
            $years = "substring_index(currentTime,'-',2) as ";
        }
        $title = "数据范围：";
        if(!empty($_GET['year'])){
            $this->node = $title."按年搜索——".$_GET['year'];
        }elseif(!empty($_GET['month'])){
            $this->node = $title."按月搜索——".$_GET['month'];
        }elseif(!empty($_GET['end_time'])){
            $this->node = $title."时间段——".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."开始时间——".$_GET['start_time'];
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bill_total")->field("sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($map)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['electricity'] = $v['electricity'] ? sprintf("%.2f", $v['electricity']) : '0.00';
            $countBill[$k]['carbon']      = sprintf("%.2f", $v['electricity'] * 1.174);
            $countBill[$k]['charge_time'] = $v['charge_time'] ? Sec2Times($v['charge_time']) : '0小时0分0秒';
            $countBill[$k]['amount']      = $v['amount'] ? sprintf("%.2f", $v['amount']) : '0.00';
            $countBill[$k]['service_fee'] = $v['service_fee'] ? sprintf("%.2f", $v['service_fee']) : '0.00';
            $countBill[$k]['total']       = $v['total'] ? $v['total'] : 0;

        }
        $this->countBills = $countBill;//汇总输出

        //分页
        $count = count(M("bill_total")->where($map)->group($year)->select());
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $site_bill = M("bill_total")->field("{$years} currentTime,sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($map)->group($year)->order("currentTime desc")->select();
        foreach($site_bill as $ks => $vs){
            $site_bill[$ks]['electricity'] = $vs['electricity'] ? sprintf("%.2f", $vs['electricity']) : '0.00';
            $site_bill[$ks]['carbon']      = sprintf("%.2f", $vs['electricity'] * 1.174);
            $site_bill[$ks]['charge_time'] = $vs['charge_time'] ? Sec2Times($vs['charge_time']) : '0小时0分0秒';
            $site_bill[$ks]['amount']      = $vs['amount'] ? sprintf("%.2f", $vs['amount']) : '0.00';
            $site_bill[$ks]['service_fee'] = $vs['service_fee'] ? sprintf("%.2f", $vs['service_fee']) : '0.00';
            $site_bill[$ks]['total']       = $vs['total'] ? $vs['total'] : 0;
        }
        $this->site_charge   = $site_bill;
        $this->display();
    }

    //指定站点下所有电桩统计记录
    public function site_evcharge(){
        $uid                = session('uid');
        $pid                = session('pid');
        $type               = session('fac_type');
        $key                = session('fac_key');
        $map['ev_plant_id'] = I("ev_plant_id");
        $mapp['site_id']    = I("ev_plant_id");
        $this->pro          = M("plant")->where("id='".$map['ev_plant_id']."'")->getField("name");
        //运营二维码
        if(!empty($_GET['evc_number'])){
            $map['evc_number']  = I("evc_number");
            $mapp['key_link']   = M("char")->where("evc_number = ".$map['evc_number'])->getField("key_link");
        }
        //设备地址
        if(!empty($_GET['key_link'])){
            $map['key_link']    = I("key_link");
            $mapp['key_link']   = I("key_link");
        }
        $title = "数据范围：";
        if(!empty($_GET['evc_number']) && !empty($_GET['key_link'])){
            $this->node = $title."运营二维码-".$_GET['evc_number']."——设备地址-".$_GET['key_link'];
        }elseif(!empty($_GET['key_link'])){
            $this->node = $title."设备地址-".$_GET['key_link'];
        }elseif(!empty($_GET['evc_number'])){
            $this->node = $title."运营二维码-".$_GET['evc_number'];
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bill_total")->field("sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($mapp)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['electricity'] = $v['electricity'] ? sprintf("%.2f", $v['electricity']) : '0.00';
            $countBill[$k]['carbon']      = sprintf("%.2f", $v['electricity'] * 1.174);
            $countBill[$k]['charge_time'] = $v['charge_time'] ? Sec2Times($v['charge_time']) : '0小时0分0秒';
            $countBill[$k]['amount']      = $v['amount'] ? sprintf("%.2f", $v['amount']) : '0.00';
            $countBill[$k]['service_fee'] = $v['service_fee'] ? sprintf("%.2f", $v['service_fee']) : '0.00';
            $countBill[$k]['total']       = $v['total'] ? $v['total'] : 0;
        }
        $this->countBills = $countBill;//汇总输出

        //分页
        $count = M("char")->where($map)->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $char = M("char")->field("id,key_link,evc_number")->where($map)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($char as $ks => $vs){
            $maps['key_link'] = $vs['key_link'];
            $bill             = M("bill_total")->field("sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($maps)->find();
            $char[$ks]['electricity'] = $bill['electricity'] ? sprintf("%.2f", $bill['electricity']) : '0.00';
            $char[$ks]['carbon']      = sprintf("%.2f", $bill['electricity'] * 1.174);
            $char[$ks]['charge_time'] = $bill['charge_time'] ? Sec2Times($bill['charge_time']) : '0小时0分0秒';
            $char[$ks]['amount']      = $bill['amount'] ? sprintf("%.2f", $bill['amount']) : '0.00';
            $char[$ks]['service_fee'] = $bill['service_fee'] ? sprintf("%.2f", $bill['service_fee']) : '0.00';
            $char[$ks]['total']       = $bill['total'] ? $bill['total'] : 0;
        }
//        P($char);
        $this->assign("plant",$char);
        $this->display("charge_pro");
    }

    //指定桩充电统计记录
    public function evchar_charge(){
        $uid       = session('uid');
        $pid       = session('pid');
        $type      = session('fac_type');
        $key       = session('fac_key');
        $char      = M("char")->field("evc_number,key_link")->where("id='".I("char_id")."'")->find();
        $this->pro = $char['evc_number'];
        $this->key_link   = $char['key_link'];
        $map['key_link']  = $char['key_link'];
        $mapp['key_link'] = $char['key_link'];
        $map['state']     = 1;
        $mapp['state']    = 1;
        $year             = "currentTime";
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime']  = array("between",array(I("start_time"),I("end_time")));
            $mapp['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime']  = array('like',I("start_time").'%');
            $mapp['currentTime'] = array('like',I("start_time").'%');
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']   = array("between",array($begin,$end));
            $mapp['currentTime']  = array("between",array($begin,$end));
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime']  = array('like',I("year").'%');
            $mapp['currentTime'] = array('like',I("year").'%');
            $year  = "substring_index(currentTime,'-',2)";
            $years = "substring_index(currentTime,'-',2) as ";
        }
        $title = "数据范围：";
        if(!empty($_GET['year'])){
            $this->node = $title."按年搜索——".$_GET['year'];
        }elseif(!empty($_GET['month'])){
            $this->node = $title."按月搜索——".$_GET['month'];
        }elseif(!empty($_GET['end_time'])){
            $this->node = $title."时间段——".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."开始时间——".$_GET['start_time'];
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bill_total")->field("sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($mapp)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['electricity'] = $v['electricity'] ? sprintf("%.2f", $v['electricity']) : '0.00';
            $countBill[$k]['carbon']      = sprintf("%.2f", $v['electricity'] * 1.174);
            $countBill[$k]['charge_time'] = $v['charge_time'] ? Sec2Times($v['charge_time']) : '0小时0分0秒';
            $countBill[$k]['amount']      = $v['amount'] ? sprintf("%.2f", $v['amount']) : '0.00';
            $countBill[$k]['service_fee'] = $v['service_fee'] ? sprintf("%.2f", $v['service_fee']) : '0.00';
            $countBill[$k]['total']       = $v['total'] ? $v['total'] : 0;
        }
        $this->countBills = $countBill;//汇总输出


        $count = count(M("bill_total")->where($map)->group($year)->select());
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $site_bill = M("bill_total")->field("{$years} currentTime,sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount")->where($map)->group($year)->order("currentTime desc")->select();
        foreach($site_bill as $i => $vs){
            $site_bill[$i]['electricity'] = $vs['electricity'] ? sprintf("%.2f", $vs['electricity']) : '0.00';
            $site_bill[$i]['carbon']      = sprintf("%.2f", $vs['electricity'] * 1.174);
            $site_bill[$i]['charge_time'] = $vs['charge_time'] ? Sec2Times($vs['charge_time']) : '0小时0分0秒';
            $site_bill[$i]['amount']      = $vs['amount'] ? sprintf("%.2f", $vs['amount']) : '0.00';
            $site_bill[$i]['service_fee'] = $vs['service_fee'] ? sprintf("%.2f", $vs['service_fee']) : '0.00';
            $site_bill[$i]['total']       = $vs['total'] ? $vs['total'] : 0;
        }
        $this->site_charge   = $site_bill;
        $this->display("site_charge");
    }

    //预约列表
    public function bespeak_list(){

        $start_time = I("start_time");
        if(!empty($_GET['end_time'])){
            $start_time  = date("Y-m-d H:s:i",strtotime($start_time));
            $end_time    = I("end_time");
            $end_time    = date("Y-m-d 23:59:59",strtotime($end_time));
            $map['time'] = array("between",array($start_time,$end_time));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['time'] = array("like","%".$start_time."%");
        }
        if(!empty($_GET['user_card'])){
            $map['user_card'] = I('user_card');
        }
        if(!empty($_GET['key_link'])){
            $map['key_link']  = I('key_link');
        }
        //分页
        $count = D("Bespeak")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $bespeak   = D("Bespeak")->where($map)->order("time desc")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach ($bespeak as $k => $v) {
            $char       = M("char")->where("key_link=".$v['key_link'])->find();
            $plant      = M("plant")->where("id=".$char['ev_plant_id'])->find();
            $arr        = strsToArray($plant['location']);
            $region     = M("region")->where("code=".$arr['2'])->find();
            $bespeak[$k]['region_text']   = $region['fullname'];
            $bespeak[$k]['plantAddr']     = $plant['name'];
            if(mb_strlen($plant['name'],"utf-8") <= 17){
                $bespeak[$k]['plantName'] = $plant['name'];
            }else{
                $bespeak[$k]['plantName'] = mb_substr($plant['name'],0,17,"utf-8")."···";
            }
        }
        $this->bespeak = $bespeak;
        $this->display();
    }

    //预约详情
    public function bespeak_detail(){

        $bespeak = D("Admin/Bespeak")->where("id=".I("id"))->find();
        $char    = M("char")->where("key_link=".$bespeak['key_link'])->find();
        $plant   = M("plant")->where("id=".$char['ev_plant_id'])->find();
        $bespeak['evc_number'] = $char['evc_number'];
        $bespeak['name']       = $plant['name'];
        $bespeak['address']    = $plant['address'];
        $this->data            = $bespeak;
        $this->display();
    }

    //预约记录统计
    public function bespeak_statis(){

        $map['state']  = 1;
        $maps['state'] = 1;
        $year          = "currentTime";
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime']  = array("between",array(I("start_time"),I("end_time")));
            $maps['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime']  = array('like',I("start_time").'%');
            $maps['currentTime'] = array('like',I("start_time").'%');
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']   = array("between",array($begin,$end));
            $maps['currentTime']  = array("between",array($begin,$end));
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $maps['currentTime'] = array('like',I("year").'%');
        }
        //区域查询
        if(!empty($_GET['location_f_1'])){

            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
            $map['location'] = array('like','%'.$location.'%');
            $mapp['location'] = array('like','%'.$location.'%');
            //将数字的地址转换成文字
            $region     = strsToArray($location);
            if($region[2] == ''){
                if($region[1] == 110100 || $region[1] == 120100 || $region[1] == 310100 || $region[1] == 500100){
                    $regionOne  = D("Admin/Region")->where("code='".$region[0]."'")->field("fullname")->find();
                }else{
                    $regionOne  = D("Admin/Region")->where("pcode='".$region[1]."'")->field("fullname")->find();
                }
            }else{
                $regionOne  = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            }
            $area = $regionOne['fullname'];
        }
        //站点名称查询
        if(!empty($_GET['name'])){
            $map['site_name'] = array('like','%'.I('name').'%');
            $mapp['name'] = array('like','%'.I('name').'%');
        }
        $title = "数据范围：";
        if(!empty($_GET['location_t_1']) && !empty($_GET['year'])){
            $this->node = $title."按城市+年搜索——".$area."--".$_GET['year'];
        }elseif(!empty($_GET['location_t_1']) && !empty($_GET['month'])){
            $this->node = $title."按城市+月搜索——".$area."--".$_GET['month'];
        }elseif(!empty($_GET['location_t_1']) && !empty($_GET['end_time'])){
            $this->node = $title."按城市+时间段——".$area."--".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['location_t_1']) && !empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."按城市+开始时间——".$area."--".$_GET['start_time'];
        }elseif(!empty($_GET['name'])){
            $this->node = $title."按站点名称——".$_GET['name'];
        }elseif(!empty($_GET['year'])){
            $this->node = $title."按年搜索——".$_GET['year'];
        }elseif(!empty($_GET['month'])){
            $this->node = $title."按月搜索——".$_GET['month'];
        }elseif(!empty($_GET['end_time'])){
            $this->node = $title."时间段——".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."开始时间——".$_GET['start_time'];
        }elseif(!empty($_GET['location_t_1'])){
            $this->node = $title."按城市搜索——".$area;
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bespeak_total")->field("sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($map)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['bespeak_time'] = $v['bespeak_time'] ? Sec2Times($v['bespeak_time']) : '0小时0分0秒';
            $countBill[$k]['bespeak_fee']  = $v['bespeak_fee'] ? sprintf("%.2f", $v['bespeak_fee']) : '0.00';
            $countBill[$k]['total']        = $v['total'] ? $v['total'] : 0;
        }
        $this->countBills = $countBill;//汇总输出

        //分页
        $count = M("plant")->where($mapp)->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $plant = M("plant")->field("id,name,location,address,total_ev")->where($mapp)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($plant as $k=>$v){
            $maps['site_id'] = $v['id'];
            //将数字的地址转换成文字
            $region = strsToArray($v['location']);
            $regionOne = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $plant[$k]['location'] = $regionOne['fullname'];
            if(mb_strlen($v['name'],"utf-8") <= 17){
                $plant[$k]['name_text'] = $v['name'];
            }else{
                $plant[$k]['name_text'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
            $sumdata = M("bespeak_total")->field("sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($maps)->select();
            foreach($sumdata as $vs){
                $plant[$k]['bespeak_time'] = $vs['bespeak_time'] ? Sec2Times($vs['bespeak_time']) : '0小时0分0秒';
                $plant[$k]['bespeak_fee']  = $vs['bespeak_fee'] ? sprintf("%.2f", $vs['bespeak_fee']) : '0.00';
                $plant[$k]['total']        = $vs['total'] ? $vs['total'] : 0;
            }
        }
        $this->china = D("Region")->getChinaArea();//全国省市区
        $this->assign("plant",$plant);//数据输出
        $this->display();
    }

    //当前站点预约详情统计
    public function bespeak_pro(){

        $id   = I("id");
        $uid  = session('uid');
        $pid  = session('pid');
        $type = session('fac_type');
        $key  = session('fac_key');
        $this->pro = M("plant")->where("id={$id}")->getField("name");
        $map['state']    = 1;
        $map['site_id']  = $id;
        $year            = "currentTime";
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = array('like',I("start_time").'%');
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $year  = "substring_index(currentTime,'-',2)";
            $years = "substring_index(currentTime,'-',2) as ";
        }
        $title = "数据范围：";
        if(!empty($_GET['year'])){
            $this->node = $title."按年搜索——".$_GET['year'];
        }elseif(!empty($_GET['month'])){
            $this->node = $title."按月搜索——".$_GET['month'];
        }elseif(!empty($_GET['end_time'])){
            $this->node = $title."时间段——".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."开始时间——".$_GET['start_time'];
        }else{
            $this->node = $title."全部";
        }

        $countBill = M("bespeak_total")->field("sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($map)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['bespeak_time'] = $v['bespeak_time'] ? Sec2Times($v['bespeak_time']) : '0小时0分0秒';
            $countBill[$k]['bespeak_fee'] = $v['bespeak_fee'] ? sprintf("%.2f", $v['bespeak_fee']) : '0.00';
            $countBill[$k]['total']       = $v['total'] ? $v['total'] : 0;

        }
        $this->countBills = $countBill;//汇总输出

        //分页
        $count = count(M("bespeak_total")->where($map)->group($year)->select());
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $site_bill = M("bespeak_total")->field("{$years} currentTime,sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($map)->group($year)->order("currentTime desc")->select();
        foreach($site_bill as $ks => $vs){
            $site_bill[$ks]['bespeak_time'] = $vs['bespeak_time'] ? Sec2Times($vs['bespeak_time']) : '0小时0分0秒';
            $site_bill[$ks]['bespeak_fee'] = $vs['bespeak_fee'] ? sprintf("%.2f", $vs['bespeak_fee']) : '0.00';
            $site_bill[$ks]['total']       = $vs['total'] ? $vs['total'] : 0;
        }
        $this->site_charge   = $site_bill;
        $this->display();
    }

    //当前站点预约次数总计
    public function bespeak_area(){

        $uid                = session('uid');
        $pid                = session('pid');
        $type               = session('fac_type');
        $key                = session('fac_key');
        $map['ev_plant_id'] = I("ev_plant_id");
        $mapp['site_id']    = I("ev_plant_id");
        $this->pro          = M("plant")->where("id='".$map['ev_plant_id']."'")->getField("name");
        //运营二维码
        if(!empty($_GET['evc_number'])){
            $map['evc_number']  = I("evc_number");
            $mapp['key_link']   = M("char")->where("evc_number = ".$map['evc_number'])->getField("key_link");
        }
        //设备地址
        if(!empty($_GET['key_link'])){
            $map['key_link']    = I("key_link");
            $mapp['key_link']   = I("key_link");
        }
        $title = "数据范围：";
        if(!empty($_GET['evc_number']) && !empty($_GET['key_link'])){
            $this->node = $title."运营二维码-".$_GET['evc_number']."——设备地址-".$_GET['key_link'];
        }elseif(!empty($_GET['key_link'])){
            $this->node = $title."设备地址-".$_GET['key_link'];
        }elseif(!empty($_GET['evc_number'])){
            $this->node = $title."运营二维码-".$_GET['evc_number'];
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bespeak_total")->field("sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($mapp)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['bespeak_time'] = $v['bespeak_time'] ? Sec2Times($v['bespeak_time']) : '0小时0分0秒';
            $countBill[$k]['bespeak_fee'] = $v['bespeak_fee'] ? sprintf("%.2f", $v['bespeak_fee']) : '0.00';
            $countBill[$k]['total']       = $v['total'] ? $v['total'] : 0;
        }
        $this->countBills = $countBill;//汇总输出

        //分页
        $count = M("char")->where($map)->count();
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $char = M("char")->field("id,key_link,evc_number")->where($map)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($char as $k => $v){
            $maps['key_link'] = $v['key_link'];
            $bill             = M("bespeak_total")->field("sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($maps)->find();
            $char[$k]['bespeak_time'] = $bill['bespeak_time'] ? Sec2Times($bill['bespeak_time']) : '0小时0分0秒';
            $char[$k]['bespeak_fee'] = $bill['bespeak_fee'] ? sprintf("%.2f", $bill['bespeak_fee']) : '0.00';
            $char[$k]['total']       = $bill['total'] ? $bill['total'] : 0;
        }
//        P($char);
        $this->assign("plant",$char);
        $this->display();
    }

    //当前电桩预约统计
    public function evchar_bespeak(){

        $uid       = session('uid');
        $pid       = session('pid');
        $type      = session('fac_type');
        $key       = session('fac_key');
        $char      = M("char")->field("evc_number,key_link")->where("id='".I("char_id")."'")->find();
        $this->pro = $char['evc_number'];
        $this->key_link   = $char['key_link'];
        $map['key_link']  = $char['key_link'];
        $mapp['key_link'] = $char['key_link'];
        $map['state']     = 1;
        $mapp['state']    = 1;
        $year             = "currentTime";
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime']  = array("between",array(I("start_time"),I("end_time")));
            $mapp['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime']  = array('like',I("start_time").'%');
            $mapp['currentTime'] = array('like',I("start_time").'%');
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']   = array("between",array($begin,$end));
            $mapp['currentTime']  = array("between",array($begin,$end));
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime']  = array('like',I("year").'%');
            $mapp['currentTime'] = array('like',I("year").'%');
            $year  = "substring_index(currentTime,'-',2)";
            $years = "substring_index(currentTime,'-',2) as ";
        }
        $title = "数据范围：";
        if(!empty($_GET['year'])){
            $this->node = $title."按年搜索——".$_GET['year'];
        }elseif(!empty($_GET['month'])){
            $this->node = $title."按月搜索——".$_GET['month'];
        }elseif(!empty($_GET['end_time'])){
            $this->node = $title."时间段——".$_GET['start_time']."~".$_GET['end_time'];
        }elseif(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $this->node = $title."开始时间——".$_GET['start_time'];
        }else{
            $this->node = $title."全部";
        }
        $countBill = M("bespeak_total")->field("sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($mapp)->select();
        //数据处理：保留两位小数并且四舍五入，将秒转换成 小时-分-秒
        foreach($countBill as $k => $v){
            $countBill[$k]['bespeak_time'] = $v['bespeak_time'] ? Sec2Times($v['bespeak_time']) : '0小时0分0秒';
            $countBill[$k]['bespeak_fee']  = $v['bespeak_fee'] ? sprintf("%.2f", $v['bespeak_fee']) : '0.00';
            $countBill[$k]['total']        = $v['total'] ? $v['total'] : 0;
        }
        $this->countBills = $countBill;//汇总输出

        $count = count(M("bespeak_total")->where($map)->group($year)->select());
//        echo M()->getLastSql();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $site_bill = M("bespeak_total")->field("{$years} currentTime,sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee")->where($map)->group($year)->order("currentTime desc")->select();
        foreach($site_bill as $i => $j){
            $site_bill[$i]['bespeak_time'] = $j['bespeak_time'] ? Sec2Times($j['bespeak_time']) : '0小时0分0秒';
            $site_bill[$i]['bespeak_fee']  = $j['bespeak_fee'] ? sprintf("%.2f", $j['bespeak_fee']) : '0.00';
            $site_bill[$i]['total']        = $j['total'] ? $j['total'] : 0;
        }
        $this->site_charge   = $site_bill;
        $this->display("bespeak_pro");
    }

    //全国所有省账单
    public function bill_statis(){

        $map['state']  = 1;
        $maps['state'] = 1;
        $year          = "currentTime";
        
        $c="state=1";
        
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime']  = array("between",array(I("start_time"),I("end_time")));
            $maps['currentTime'] = array("between",array(I("start_time"),I("end_time")));
            $st=I("start_time");
            $et=I("end_time");
            $c="{$c} and currentTime between '{$st}' and '{$et}'";
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime']  = array('like',I("start_time").'%');
            $maps['currentTime'] = array('like',I("start_time").'%');
        
            $st=I("start_time");
        
            $c="{$c} and currentTime like '{$st}%'";
        
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']   = array("between",array($begin,$end));
            $maps['currentTime']  = array("between",array($begin,$end));
        
            $c="{$c} and currentTime between '{$begin}' and '{$end}'";
        
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $maps['currentTime'] = array('like',I("year").'%');
        
            $ye=I("year");
            $c="{$c} and currentTime like '{$ye}%'";
        
        }
        //区域查询
        if(!empty($_GET['location_f_1'])){
        
            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
            $map['location'] = array('like','%'.$location.'%');
            $mapp['location'] = array('like','%'.$location.'%');
        
            $c="{$c} and location like '%{$location}%'";
        
        }
        //站点名称查询
        if(!empty($_GET['site_name'])){
            $map['site_name'] = array('like','%'.I('site_name').'%');
            $mapp['site_name'] = array('like','%'.I('site_name').'%');
        
            $na=I('site_name');
            $c="{$c} and site_name like '%{$na}%'";
        
        }
        
        
/*         $c="state=1";
        $map['state']    = 1;

        $year            = "currentTime";
        //区域查询
        if(!empty($_GET['location_s_1'])){
            $map['location'] = array('like','%'.I("location_s_1").'%');
            $loca=I("location_s_1");
            $c="{$c} and location like '%{$loca}%'";
            //$this->pro       = M("region")->where("code = '".I("location_s_1")."'")->getField("fullname");
        }
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
            $st=I("start_time");
            $et=I("end_time");
            
            $c="{$c} and currentTime between '{$st}' and '{$et}'";
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = array('like',I("start_time").'%');
            $st=I("start_time");
            $c="{$c} and currentTime like '{$et}%'";
            
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
            
            $c="{$c} and currentTime between '{$begin}' and '{$end}'";
            
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $ye=I("year");
            $c="{$c} and currentTime like '{$ye}%'";
            
            $year  = "substring_index(currentTime,'-',2)";
            $years = "substring_index(currentTime,'-',2) as ";
            
            
        } */

        
        $countBill     = M("bill_total")->field("sum(total) as total,sum(charge_time)  as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where($c)->select();

/*         echo M()->getLastSql();
        die; */
        
        $countBespeak  =M("bespeak_total")->field("sum(total) as total,sum(bespeak_time)  as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where($c)->select();
         

 /*        
        //2---厂商  3---运营商
        if(session("fac_type") == 2){
            if (session('fac_key')){
                $countBill     = M("bill_total")->field("sum(total) as total,sum(charge_time  as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("fac_id='".session('uid')."' and {$c}")->select();
                $countBespeak  =M("fac_bespeak_total")->field("sum(total) as total,sum(bespeak_time)  as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("fac_id='".session('uid')."' and  {$c}")->select();
                //echo M()->getLastSql();
            }else{
                $countBill     = M("fac_bill_total")->field("sum(total) as total,sum(charge_time)   as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("(fac_id='".session('uid')."' OR fac_pid='".session('uid')."') and  {$c}")->select();
                //echo M()->getLastSql();
                $countBespeak  =M("fac_bespeak_total")->field("sum(total) as total,sum(bespeak_time)   as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("(fac_id='".session('uid')."' OR fac_pid='".session('uid')."') and  {$c}")->select();
                //echo M()->getLastSql();
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $countBill     = M("oper_bill_total")->field("sum(total) as total,sum(charge_time)   as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("oper_id='".session('uid')."' and  {$c}")->select();
                $countBespeak  =M("oper_bespeak_total")->field("sum(total) as total,sum(bespeak_time)   as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("oper_id='".session('uid')."' and  {$c}")->select();
            
            }else{
                $countBill     = M("oper_bill_total")->field("sum(total) as total,sum(charge_time)  as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("(oper_id='".session('uid')."' OR oper_pid='".session('uid')."') and  {$c}")->select();
                $countBespeak  =M("oper_bespeak_total")->field("sum(total) as total,sum(bespeak_time)   as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("(oper_id='".session('uid')."' OR oper_pid='".session('uid')."') and  {$c}")->select();
                
            }
        } */
        
        foreach($countBespeak as $v){
           
     
            $sv['bespeak_fee']=$v['bespeak_fee'];
        
            $sv['bespeak_time']=$v['bespeak_time'];


        }
        
     
        
        
        
        
        foreach($countBill as $v){
            $vv=$v;
            $vv['carbon']=$v['electricity'] * 1.174;
            $vv['bespeak_fee']=$sv['bespeak_fee'];
            
            $vv['bespeak_time']=$sv['bespeak_time'];
            $vv['total_time']=$v['charge_time']+$sv['bespeak_time'];
            $vv['total_amount']=$v['amount']+$sv['bespeak_fee']+ $v['service_fee'];
            
            $vv['total_time_str']=Sec2Times($vv['total_time']);
            $vv['bespeak_time_str']=Sec2Times($sv['bespeak_time']);
            $vv['charge_time_str']=Sec2Times($v['charge_time']);
            
            //echo $countBespeak['bespeak_time'];
            
            
        }
        $this->countBill = $vv;
        

        
        //$china = D("Region")->where("level=2")->select();
        //$this->china = $china;
        
        $this->china = D("Admin/Region")->getChinaArea();//全国省市区
        
        $this->display();
    }
    
    public function bill_site(){
    
        $map['state']  = 1;
        $maps['state'] = 1;
        $year          = "currentTime";
    
        $c="state=1";
    
        //时间段查询
        if(!empty($_GET['end_time'])){
            $map['currentTime']  = array("between",array(I("start_time"),I("end_time")));
            $maps['currentTime'] = array("between",array(I("start_time"),I("end_time")));
            $st=I("start_time");
            $et=I("end_time");
            $c="{$c} and currentTime between '{$st}' and '{$et}'";
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime']  = array('like',I("start_time").'%');
            $maps['currentTime'] = array('like',I("start_time").'%');
    
            $st=I("start_time");
    
            $c="{$c} and currentTime like '{$st}%'";
    
        }
        //按月查询
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']   = array("between",array($begin,$end));
            $maps['currentTime']  = array("between",array($begin,$end));
    
            $c="{$c} and currentTime between '{$begin}' and '{$end}'";
    
        }
        //按年查询
        if(!empty($_GET['year'])){
            $map['currentTime'] = array('like',I("year").'%');
            $maps['currentTime'] = array('like',I("year").'%');
    
            $ye=I("year");
            $c="{$c} and currentTime like '{$ye}%'";
    
        }
        //区域查询
        if(!empty($_GET['location_f_1'])){
    
            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
            $map['location'] = array('like','%'.$location.'%');
            $mapp['location'] = array('like','%'.$location.'%');
    
            $c="{$c} and location like '%{$location}%'";
    
        }
        //站点名称查询
        if(!empty($_GET['site_name'])){
            $map['site_name'] = array('like','%'.I('site_name').'%');
            $mapp['site_name'] = array('like','%'.I('site_name').'%');
    
            $na=I('site_name');
            $c="{$c} and site_name like '%{$na}%'";
    
        }
    
        $bill_field="sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount,count(id) as char_count,site_name,location,site_id";
        $bes_field="sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee,site_name,location,site_id";
    
        $bill_field_2="sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount";
        $bes_field_2="sum(total) as total_bespeak,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee";
         
    
        /*      $countBill     = M("bill_total")->field($bill_field)->where($c)->group("site_id")->select();
    
    
    
    
        $countBespeak  =M("bespeak_total")->field($bes_field)->where($c)->group("site_id")->select();
    
        $huizongBill  = M("bill_total")->field($bill_field_2)->where($c)->select();
        $huizongBespeak  =M("bespeak_total")->field($bes_field_2)->where($c)->select();  */
    
    
        //分页
        $count =count(M("bill_total")->field($bill_field)->where($c)->group("site_id")->select());
    
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
    
        // $plant = M("plant")->where($map)->order("id")->page($nowPage . ',' . $Page->listRows)->select();
        $countBill     = M("bill_total")->field($bill_field)->where($c)->group("site_id")->page($nowPage . ',' . $Page->listRows)->select();
        $countBespeak  =M("bespeak_total")->field($bes_field)->where($c)->group("site_id")->page($nowPage . ',' . $Page->listRows)->select();
    
        // $huizongBill  = M("bill_total")->field($bill_field_2)->where($c)->page($nowPage . ',' . $Page->listRows)->select();
        //$huizongBespeak  =M("bespeak_total")->field($bes_field_2)->where($c)->page($nowPage . ',' . $Page->listRows)->select();
    
        //        echo M()->getLastSql();
        //分页结束
    
    
         
    
        foreach($countBespeak as $k => $v){
    
            $bes[$v['site_id']]['bespeak_fee']=$v['bespeak_fee'];
            $bes[$v['site_id']]['bespeak_time']=$v['bespeak_time'];
    
             
        }
    
        foreach($countBill as $k=> $v){
            $plantBill[$k]=$v;
             
            $plantBill[$k]['carbon']=$v['electricity'] * 1.174;
            $plantBill[$k]['bespeak_time']=$bes[$v['site_id']]['bespeak_time'];
            $plantBill[$k]['bespeak_fee']=$bes[$v['site_id']]['bespeak_fee'];
    
            $plantBill[$k]['total_time']=$v['charge_time']+$bes[$v['site_id']]['bespeak_time'];
            $plantBill[$k]['total_amount']=$v['amount']+$bes[$v['site_id']]['bespeak_fee']+ $v['service_fee'];
    
            $plantBill[$k]['total_time_str']=Sec2Times($plantBill[$k]['total_time']);
            $plantBill[$k]['bespeak_time_str']=Sec2Times($bes[$v['site_id']]['bespeak_time']);
            $plantBill[$k]['charge_time_str']=Sec2Times($plantBill[$k]['charge_time']);
    
    
    
            if(mb_strlen($plantBill[$k]['site_name'],"utf-8") <= 5){
                $plantBill[$k]['site_name'] = $plantBill[$k]['site_name'];
            }else{
                $plantBill[$k]['site_name'] = mb_substr($plantBill[$k]['site_name'],0,5,"utf-8")."···";
            }
    
    
            //将数字的地址转换成文字
            $region = strsToArray($v['location']);
            $regionOne = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $plantBill[$k]['location'] = $regionOne['fullname'];
             
    
    
            if(mb_strlen($plantBill[$k]['location'],"utf-8") <= 5){
                $plantBill[$k]['location'] = $plantBill[$k]['location'];
            }else{
                $plantBill[$k]['location'] = mb_substr($plantBill[$k]['location'],0,5,"utf-8")."···";
            }
    
        }
    
        //一下为汇总统计
        $bill_field_2="sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount";
        $bes_field_2="sum(total) as total_bespeak,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee";
         
    
        /*         foreach($huizongBill as $k=>$v){
         $huizong['total']=$huizong['total']+$v['total'];
         $huizong['charge_time']=$v['charge_time'];
         $huizong['electricity']=$v['electricity'];
         $huizong['electricity']=$v['electricity'];
         $huizong['service_fee']=  !$v['service_fee']?0:$v['service_fee'];
         $huizong['amount']=$v['amount'];
    
    
    
    
         } */
    
    
    
        //$huizong=array_merge($b)
        foreach($countBill as $k => $v){
            $huizong['total']=$huizong['total']+$v['total'];
            $huizong['charge_time']=$huizong['charge_time']+$v['charge_time'];
            $huizong['electricity']=$huizong['electricity']+$v['electricity'];
            //$huizong['carbon']=$v['electricity'] ;
            // $huizong['electricity']=$huizong['electricity']+$v['electricity'] ;
            $huizong['service_fee']=$huizong['service_fee']  +$v['service_fee'];
            $huizong['amount']=$huizong['amount']+$v['amount'];
            $huizong['charge_time']=$huizong['charge_time']+$v['charge_time'];
    
    
            //$huizong['charge_time_str']=Sec2Times($v['charge_time']);
    
             
    
        }
        //var_dump($huizong);
        foreach($countBespeak as $k => $v){
            $huizong['total_bespeak']=$huizong['total_bespeak']+$v['total'];
            $huizong['bespeak_time']=$huizong['bespeak_time']+$v['bespeak_time'];
            $huizong['bespeak_fee']=$huizong['bespeak_fee']+$v['bespeak_fee'];
    
            //$huizong['bespeak_time_str']=Sec2Times($v['bespeak_time']);
    
    
        }
    
        $huizong['total_amount']=$huizong['amount']+$huizong['bespeak_fee']+ $huizong['service_fee'];
         
    
    
        $huizong['carbon']=$huizong['electricity'] * 1.174;
    
    
        //$huizong['total_amount']=$huizong['amount']+$huizong['bespeak_fee']+ $huizong['service_fee'];
    
        $huizong['bespeak_time_str']=Sec2Times($huizong['bespeak_time']);
        $huizong['charge_time_str']=Sec2Times($huizong['charge_time']);
    
    
    
    
    
        //
        $this->china = D("Admin/Region")->getChinaArea();//全国省市区
    
    
        $this->assign("huizong",$huizong);//数据输出
    
        $this->assign("plant",$plantBill);//数据输出
    
    
    
        $this->display();
    }
    
    
    
    
    //当前省的账单次数总计
    public function bill_pro(){

        $map['location'] = array('like',I("code").'%');
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = I("start_time");
        }
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
        }
        //分页
        $count = M("pro_bill_total")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $this->billPro   = M("pro_bill_total")->where($map)->order("currentTime desc")->page($nowPage . ',' . $Page->listRows)->select();
        //echo M("pro_charge_total")->getLastSql();
        //分页结束
        $this->display();
    }
    //全省各市
    public function bill_city(){
        $china = D("Region")->getCity(I("code"));
        foreach($china as $v){
            $this->city = $v;
        }
        $this->display();
    }

    //当前市的账单次数总计
    public function bill_area(){

        $map['location'] = array('like','%'.I("code").'%');
        if(!empty($_GET['end_time'])){
            $map['currentTime'] = array("between",array(I("start_time"),I("end_time")));
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $map['currentTime'] = I("start_time");
        }
        if(!empty($_GET['month'])){
            $tomonth   = I("month");//某月
            $begin     = date("Y-m-01",strtotime($tomonth));//某月的第一天
            $end       = date('Y-m-d', strtotime("$begin +1 month -1 day"));//某月的最后一天
            $map['currentTime']  = array("between",array($begin,$end));
        }
        //分页
        $count = M("bill_total")->where($map)->count();
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page     = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage  = isset($_GET['page']) ? $_GET['page'] : 1;
        $this->billcity   = M("bill_total")->where($map)->order("currentTime desc")->page($nowPage . ',' . $Page->listRows)->select();
        //echo M("pro_charge_total")->getLastSql();
        //分页结束
        $this->display();
    }

    //数据导出
    public function exportBill(){
        $yearmonth=$_REQUEST['yearmonth'];
    
        //
        //$yearmonth="2015-08";
         
        $yearmonth=$this->getLastMonth();
    
    
        $xlsCell_A  = array(
    
            array('total','账单数(次)'),
            array('electricity','总用电量'),
            array('carbon','减少碳排放量(千克)'),
            array('total_time_str','总时长(时/分/秒)'),
            array('bespeak_time_str','预约时长(时/分/秒)'),
            array('charge_time_str','充电时长(时/分/秒)'),
            array('total_amount','总金额(元)'),
            array('bespeak_fee','预约费(元)'),
            array('amount','充电费(元)'),
            array('service_fee','服务费(元)'),
    
    
        );
    
        $xlsCell_B  = array(
    
            array('site_id','ID'),
            array('site_name','站点名称'),
            array('location','所在地'),
            array('char_count','电桩数'),
            array('total','账单数(次)'),
            array('electricity','总用电量(度)'),
            array('carbon','碳排放量(千克)'),
            array('total_time_str','总时长(时/分/秒)'),
            array('bespeak_time_str','预约时长(时/分/秒)'),
            array('charge_time_str','充电时长(时/分/秒)'),
            array('total_amount','总金额(元)'),
    
            array('bespeak_fee','预约费(元)'),
            array('amount','充电费(元)'),
            array('service_fee','服务费(元)'),
    
    
        );
    
        /*
         //汇总数据
         $c="currentTime like '{$yearmonth}%'";
         //2---厂商  3---运营商
         if(session("fac_type") == 2){
         if (session('fac_key')){
         $countBill     = M("fac_bill_total")->field("sum(total) as total,sum(charge_time  as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("fac_id='".session('uid')."' and {$c}")->select();
         $countBespeak  =M("fac_bespeak_total")->field("sum(total) as total,sum(bespeak_time)  as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("fac_id='".session('uid')."' and  {$c}")->select();
         //echo M()->getLastSql();
         }else{
         $countBill     = M("fac_bill_total")->field("sum(total) as total,sum(charge_time)   as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("(fac_id='".session('uid')."' OR fac_pid='".session('uid')."') and  {$c}")->select();
         //echo M()->getLastSql();
         $countBespeak  =M("fac_bespeak_total")->field("sum(total) as total,sum(bespeak_time)   as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("(fac_id='".session('uid')."' OR fac_pid='".session('uid')."') and  {$c}")->select();
         //echo M()->getLastSql();
         }
         }elseif(session("fac_type") == 3){
         if(session('pid') != 0){
         $countBill     = M("oper_bill_total")->field("sum(total) as total,sum(charge_time)   as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("oper_id='".session('uid')."' and  {$c}")->select();
         $countBespeak  =M("oper_bespeak_total")->field("sum(total) as total,sum(bespeak_time)   as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("oper_id='".session('uid')."' and  {$c}")->select();
    
         }else{
         $countBill     = M("oper_bill_total")->field("sum(total) as total,sum(charge_time)  as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("(oper_id='".session('uid')."' OR oper_pid='".session('uid')."') and  {$c}")->select();
         $countBespeak  =M("oper_bespeak_total")->field("sum(total) as total,sum(bespeak_time)   as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where("(oper_id='".session('uid')."' OR oper_pid='".session('uid')."') and  {$c}")->select();
    
         }
         }
    
    
    
    
         $countBill     = M("bill_total")->field("sum(total) as total,sum(charge_time  as charge_time,sum(electricity) as electricity,sum(amount) as amount,sum(service_fee) as service_fee")->where("fac_id='".session('uid')."' and {$c}")->select();
         $countBespeak  =M("bespeak_total")->field("sum(total) as total,sum(bespeak_time)  as bespeak_time ,sum(bespeak_fee) as bespeak_fee")->where($c)->select();
    
        */
    
    
        /*
         foreach($countBespeak as $v){
          
          
         $sv['bespeak_fee']=$v['bespeak_fee'];
    
         $sv['bespeak_time']=$v['bespeak_time'];
    
    
        } */
    
      
    
    
    
    
        /*       foreach($countBill as $v){
         $vv=$v;
         $vv['carbon']=$v['electricity'] * 1.174;
         $vv['bespeak_fee']=$sv['bespeak_fee'];
    
         $vv['bespeak_time']=$sv['bespeak_time'];
         $vv['total_time']=$v['charge_time']+$sv['bespeak_time'];
         $vv['total_amount']=$v['amount']+$sv['bespeak_fee']+ $v['service_fee'];
    
         $vv['total_time_str']=Sec2Times($vv['total_time']);
         $vv['bespeak_time_str']=Sec2Times($sv['bespeak_time']);
         $vv['charge_time_str']=Sec2Times($v['charge_time']);
    
         $vv['service_fee']=!$vv['service_fee']?0:$vv['service_fee'];
         //echo $countBespeak['bespeak_time'];
    
    
        } */
    
    
        //
    
    
        $c="currentTime like '{$yearmonth}%'";
    
        $bill_field="sum(total) as total,sum(charge_time) as charge_time,sum(electricity) as electricity,sum(service_fee) as service_fee,sum(amount) as amount,count(id) as char_count,site_name,location,site_id";
        $bes_field="sum(total) as total,sum(bespeak_time) as bespeak_time,sum(bespeak_fee) as bespeak_fee,site_name,location,site_id";
    
    
        $countBill     = M("bill_total")->field($bill_field)->where($c)->group("site_id")->select();
    
        $countBespeak  =M("bespeak_total")->field($bes_field)->where($c)->group("site_id")->select();
    
        foreach($countBill as $k => $v){
            $huizong['total']=$huizong['total']+$v['total'];
            $huizong['charge_time']=$huizong['charge_time']+$v['charge_time'];
            $huizong['electricity']=$huizong['electricity']+$v['electricity'];
            //$huizong['carbon']=$v['electricity'] ;
            // $huizong['electricity']=$huizong['electricity']+$v['electricity'] ;
            $huizong['service_fee']=$huizong['service_fee']  +$v['service_fee'];
            $huizong['amount']=$huizong['amount']+$v['amount'];
            $huizong['charge_time']=$huizong['charge_time']+$v['charge_time'];
    
    
            //$huizong['charge_time_str']=Sec2Times($v['charge_time']);
    
             
    
        }
        //var_dump($huizong);
        foreach($countBespeak as $k => $v){
            //$huizong['bespeak_time']=$huizong['bespeak_time']+$v['total'];
            $huizong['bespeak_time']=$huizong['bespeak_time']+$v['bespeak_time'];
            $huizong['bespeak_fee']=$huizong['bespeak_fee']+$v['bespeak_fee'];
    
            //$huizong['bespeak_time_str']=Sec2Times($v['bespeak_time']);
    
    
        }
    
        $huizong['total_amount']=$huizong['amount']+$huizong['bespeak_fee']+ $huizong['service_fee'];
         
        $huizong['total_time']=$huizong['bespeak_time']+$huizong['charge_time'];
    
    
    
        $huizong['carbon']=$huizong['electricity'] * 1.174;
    
    
        //$huizong['total_amount']=$huizong['amount']+$huizong['bespeak_fee']+ $huizong['service_fee'];
    
        $huizong['bespeak_time_str']=Sec2Times($huizong['bespeak_time']);
        $huizong['charge_time_str']=Sec2Times($huizong['charge_time']);
        $huizong['total_time_str']=Sec2Times($huizong['total_time']);
    
    
    
    
        foreach($countBespeak as $k => $v){
    
            $bes[$v['site_id']]['bespeak_fee']=$v['bespeak_fee'];
            $bes[$v['site_id']]['bespeak_time']=$v['bespeak_time'];
    
             
        }
    
        foreach($countBill as $k=> $v){
            $plantBill[$k]=$v;
             
            $plantBill[$k]['carbon']=$v['electricity'] * 1.174;
            $plantBill[$k]['bespeak_time']=$bes[$v['site_id']]['bespeak_time'];
            $plantBill[$k]['bespeak_fee']=$bes[$v['site_id']]['bespeak_fee'];
    
            $plantBill[$k]['total_time']=$v['charge_time']+$bes[$v['site_id']]['bespeak_time'];
            $plantBill[$k]['total_amount']=$v['amount']+$bes[$v['site_id']]['bespeak_fee']+ $v['service_fee'];
    
            $plantBill[$k]['total_time_str']=Sec2Times($plantBill[$k]['total_time']);
            $plantBill[$k]['bespeak_time_str']=Sec2Times($bes[$v['site_id']]['bespeak_time']);
            $plantBill[$k]['charge_time_str']=Sec2Times($plantBill[$k]['charge_time']);
    
            $plantBill[$k]['service_fee']=!$plantBill[$k]['service_fee']?0:$plantBill[$k]['service_fee'];
    
            //将数字的地址转换成文字
            $region = strsToArray($v['location']);
            $regionOne = D("Admin/Region")->where("code='".$region[2]."'")->field("fullname")->find();
            $plantBill[$k]['location'] = $regionOne['fullname'];
             
             
        }
    
        /*         echo count($countBill);
         die; */
        //var_dump($plantBill);
    
    
    
    
    
    
    
    
    
    
    
        $this->exportExcel($xlsCell_A,$huizong,$xlsCell_B,$plantBill,$yearmonth);
    }
    
    
    
    
    
    
    
    
    //PHPExcel导出方法
    private  function exportExcel($expCellName_A,$expTableData_A,$expCellName_B,$expTableData_B,$yearmonth){
    
        /*         var_dump($expTableData_A);
         die; */
    
    
        //$fileName = "Bill".date('_YmdHis',time());
        $fileName = "Bill_" .  date('Ym',strtotime($yearmonth))  . date('dHis',time());
    
        $company="北京电庄科技有限公司";
    
        $cellNum_A  = count($expCellName_A);
        $dataNum_A  = count($expTableData_A);
    
        $cellNum_B  = count($expCellName_B);
        $dataNum_B  = count($expTableData_B);
    
    
        /*         echo $dataNum_A;
         die; */
    
    
        Vendor('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel ();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
    
        //填入主标题
        $objPHPExcel->getActiveSheet()->setCellValue('A1', $company);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A3:AN3')->getFont()->setBold(true);
    
        //填入副标题
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "账单数据总览({$yearmonth})");
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(16);
    
        //设置表头行高
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(35);
        $objPHPExcel->getActiveSheet()->getRowDimension(2)->setRowHeight(22);
        $objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(25);
    
        //设置居中
        $objPHPExcel->getActiveSheet()->getStyle('A1:AN1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2:AN2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
        //合并单元格
        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
        $objPHPExcel->getActiveSheet()->mergeCells('A2:G2');
    
        /*
         $objPHPExcel->getActiveSheet()->mergeCells("A1:{$cellName[$cellNum_A-1]}1");
        $objPHPExcel->getActiveSheet()->mergeCells("A2:{$cellName[$cellNum_A-1]}2"); */
    
    
    
    
        //设置自动换行
        //        $objPHPExcel->getActiveSheet()->getStyle('A3:AK3')->getAlignment()->setWrapText(true);
    
        for($i=0;$i<$cellNum_A;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[0]. ($i+3), $expCellName_A[$i][1]);
        }
    
        for($i=0;$i<$dataNum_A;$i++){
            for($j=0;$j<$cellNum_A;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[1].($j+3), $expTableData_A[$expCellName_A[$j][0]]);
                $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[1].($j+3),$expTableData_A[$expCellName_A[$j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
            }
        }
    
        ///////
    
    
    
    
        $objPHPExcel->createSheet();
        $objPHPExcel->setActiveSheetIndex(1);
    
    
        //填入主标题
        $objPHPExcel->getActiveSheet(1)->setCellValue('A1', $company);
        $objPHPExcel->getActiveSheet(1)->getStyle('A1')->getFont()->setName('黑体');
        $objPHPExcel->getActiveSheet(1)->getStyle('A1')->getFont()->setSize(20);
        $objPHPExcel->getActiveSheet(1)->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet(1)->getStyle('A3:AN3')->getFont()->setBold(true);
    
        //填入副标题
        $objPHPExcel->getActiveSheet(1)->setCellValue('A2', "站点账单总览({$yearmonth})");
        $objPHPExcel->getActiveSheet(1)->getStyle('A2')->getFont()->setName('宋体');
        $objPHPExcel->getActiveSheet(1)->getStyle('A2')->getFont()->setSize(16);
    
        //设置表头行高
        $objPHPExcel->getActiveSheet(1)->getRowDimension(1)->setRowHeight(35);
        $objPHPExcel->getActiveSheet(1)->getRowDimension(2)->setRowHeight(22);
        $objPHPExcel->getActiveSheet(1)->getRowDimension(3)->setRowHeight(25);
    
        //设置居中
        $objPHPExcel->getActiveSheet(1)->getStyle('A1:AN1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet(1)->getStyle('A2:AN2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
        //合并单元格
    
    
        /*         var_dump($expTableData_B);
         die; */
        $objPHPExcel->getActiveSheet(1)->mergeCells("A1:{$cellName[$cellNum_B-1]}1");
        $objPHPExcel->getActiveSheet(1)->mergeCells("A2:{$cellName[$cellNum_B-1]}2");
    
    
        for($i=0;$i<$cellNum_B;$i++){
            $objPHPExcel->setActiveSheetIndex(1)->setCellValue($cellName[$i].'3', $expCellName_B[$i][1]);
        }
    
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum_B;$i++){
            for($j=0;$j<$cellNum_B;$j++){
                $objPHPExcel->getActiveSheet(1)->setCellValue($cellName[$j].($i+4), $expTableData_B[$i][$expCellName_B[$j][0]]);
                $objPHPExcel->getActiveSheet(1)->setCellValueExplicit($cellName[$j].($i+4),$expTableData_B[$i][$expCellName_B[$j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
            }
        }
    
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$fileName.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
    
    private function getLastMonth(){
        return date("Y-m",strtotime("last month"));
    }
      
    
}